SparkSession - Spark SQL 的 入口

SparkSession - Spark SQL 的 入口

翻译自:https://jaceklaskowski.gitbooks.io/mastering-apache-spark/content/spark-sql-SparkSession.html

概述

SparkSession 是 Spark SQL 的入口。使用 Dataset 或者 Datafram 编写 Spark SQL 应用的时候,第一个要创建的对象就是 SparkSession。

Note:在 Spark 2.0 中, SparkSession 合并了 SQLContext 和 HiveContext。


你可以通过 SparkSession.builder 来创建一个 SparkSession 的实例,并通过 stop 函数来停止 SparkSession。

import org.apache.spark.sql.SparkSession
val spark: SparkSession = SparkSession.builder
  .appName("My Spark Application")  // optional and will be autogenerated if not specified
  .master("local[*]")               // avoid hardcoding the deployment environment
  .enableHiveSupport()              // self-explanatory, isn't it?
  .config("spark.sql.warehouse.dir", "target/spark-warehouse")
  .getOrCreate

你可以在一个 Spark 应用中使用多个 SparkSession, 这样子就可以通过 SparSession 将多个关系实体隔离开来(可以参考 catalog 属性)。

scala> spark.catalog.listTables.show
+------------------+--------+-----------+---------+-----------+
|              name|database|description|tableType|isTemporary|
+------------------+--------+-----------+---------+-----------+
|my_permanent_table| default|       null|  MANAGED|      false|
|              strs|    null|       null|TEMPORARY|       true|
+------------------+--------+-----------+---------+-----------+

在 SparkSession 的内部, 包含了SparkContext, SharedState,SessionState 几个对象。下表中介绍了每个对象的大体功能:
Name Type Description
sparkContext SparkContext spark功能的主要入口点。可以通过 sparkConext在集群上创建RDD, accumulators 和 broadcast variables
existingSharedState Option[SharedState] 一个内部类负责保存不同session的共享状态
parentSessionState Option[SessionState] 复制父session的状态

下图是 SparkSession 的类和方法, 这些方法包含了创建 DataSet, DataFrame, Streaming 等等。
Method Description
builder "Opens" a builder to get or create a SparkSession instance
version Returns the current version of Spark.
implicits Use import spark.implicits._ to import the implicits conversions and create Datasets from (almost arbitrary) Scala objects.
emptyDataset[T] Creates an empty Dataset[T].
range Creates a Dataset[Long].
sql Executes a SQL query (and returns a DataFrame).
udf Access to user-defined functions (UDFs).
table Creates a DataFrame from a table.
catalog Access to the catalog of the entities of structured queries
read Access to DataFrameReader to read a DataFrame from external files and storage systems.
conf Access to the current runtime configuration.
readStream Access to DataStreamReader to read streaming datasets.
streams Access to StreamingQueryManager to manage structured streaming queries.
newSession Creates a new SparkSession.
stop Stops the SparkSession.


Builder

Builder 是 SparkSession 的构造器。 通过 Builder, 可以添加各种配置。
Builder 的方法如下:

Method Description
getOrCreate 获取或者新建一个 sparkSession
enableHiveSupport 增加支持 hive Support
appName 设置 application 的名字
config 设置各种配置
import org.apache.spark.sql.SparkSession
val spark: SparkSession = SparkSession.builder
  .appName("My Spark Application")  // optional and will be autogenerated if not specified
  .master("local[*]")               // avoid hardcoding the deployment environment
  .enableHiveSupport()              // self-explanatory, isn't it?
  .getOrCreate


ShareState

ShareState 是 SparkSession 的一个内部类,负责保存多个有效session之间的共享状态。下表介绍了ShareState的属性。

Name Type Description
cacheManager CacheManager 这个是 SQLContext 的支持类,会自动保存 query 的查询结果。这样子查询在执行过程中,就可以使用这些查询结果
externalCatalog ExternalCatalog 保存外部系统的 catalog
globalTempViewManager GlobalTempViewManager 一个线程安全的类,用来管理 global temp view, 并提供 create , update , remove 的等原子操作,来管理这些 view
jarClassLoader NonClosableMutableURLClassLoader 加载用户添加的 jar 包
listener SQLListener 一个监听类
sparkContext SparkContext Spark 的核心入口类
warehousePath String MetaStore 的地址,可以通过 spark.sql.warehouse.dir 或者 hive-site.xml 中的 hive.metastore.warehouse.dir 来指定, Spark 会覆盖 hive 的参数

ShareState 会使用一个 sparkContext 作为构造参数。如果可以在 CLASSPATH 中找到 hive-site.xml,ShareState 会将它加入到 sparkContext 的 hadoop configuration 中。

通过设置 log4j.logger.org.apache.spark.sql.internal.SharedState=INFO 可以看到相应的日志。

posted on 2017-09-06 16:21  walkwalkwalk  阅读(13533)  评论(0编辑  收藏  举报

导航